package com.playfair.thread;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;

import com.playfair.components.Digraph;
import com.playfair.components.Matrix;
import com.playfair.core.AppCore;
import com.playfair.thread.utils.BruteForceAttackResult;

public class BruteForceAttacker implements Runnable {

	private Thread runner;
	private String workFile;

	public BruteForceAttacker(String file, int nmb, int filen) {
		runner = new Thread(this, file + nmb + "\\" + filen);
		this.workFile = file;
		System.out.println("THREAD CREATED: " + runner.getName());
	}

	@Override
	public void run() {
		Matrix currentMatrix, bestMatrix = null;
		String line, line2, currentPlainText = null;
		Digraph digraph = new Digraph(AppCore.getInstance().getCodeToBreak());// ==========
		FileInputStream fis = null, fis2 = null;
		BufferedReader br = null, br2 = null;
		Integer nmb = 0, bestNmb = 1;
		ArrayList<String> containedWords;

		try {
			fis2 = new FileInputStream(runner.getName());
			br2 = new BufferedReader(new InputStreamReader(fis2,
					Charset.forName("UTF-8")));

			while ((line = br2.readLine()) != null) {
				line = line.toUpperCase();
				nmb = 0;
				containedWords = new ArrayList<String>();
				currentMatrix = new Matrix(line.trim());
				String plainText = AppCore.getInstance().decrypt(currentMatrix,
						digraph);
				plainText = new String(plainText);
				fis = new FileInputStream(workFile + "uwl");
				br = new BufferedReader(new InputStreamReader(fis,
						Charset.forName("UTF-8")));
				while ((line2 = br.readLine()) != null) {
					line2 = line2.trim();
					line2 = line2.toUpperCase();
					currentPlainText = plainText;
					if (plainText.contains(line2)) {
						containedWords.add(line2);
					}
				}
				while (true) {
					if (containedWords.size() == 0) {
						break;
					}
					String best = "";
					for (String str : containedWords) {
						if (str.length() > best.length())
							best = str;
					}
					if (plainText.contains(best)) {
						plainText = plainText.replace(best, "");
						nmb++;
					} else {
						containedWords.remove(best);
					}
				}
				if (AppCore.getInstance().getBestPlainText() == null
						|| plainText.length() < AppCore.getInstance()
								.getBestPlainText().length()) {
					bestNmb = nmb;
					AppCore.getInstance().setBestPlainText(plainText);
					bestMatrix = currentMatrix;
					AppCore.getInstance()
							.getPossibleSolutions()
							.add(new BruteForceAttackResult("key: "
									+ bestMatrix.getKey() + ", text: "
									+ currentPlainText, new Integer(plainText
									.length())));
					System.out.println("FOUND: " + plainText + " " + line + " "
							+ bestNmb + " " + runner.getName());
				}
			}
			br.close();
			br2.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("THREAD DONE: " + runner.getName());

	}

}
